package com.game.commands
{
	import com.game.views.UIMediator;
	import com.module.regrole.RegRoleManager;
	import com.module.ui.UiManager;
	
	import flash.events.Event;
	
	import mx.core.Application;
	import mx.events.ModuleEvent;
	import mx.modules.ModuleLoader;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	public class EnterGameCommand extends SimpleCommand
	{
		public static const NAME:String = "EnterGameCommand";
		public function EnterGameCommand()
		{
			super();
		}
		override public function execute(notification:INotification):void
		{
			if(this.facade.hasMediator(UIMediator.NAME)){
				this.facade.removeMediator(UIMediator.NAME);
			}
			if(UiManager.uiPanel && UiManager.uiPanel.parent){
				UiManager.uiPanel.parent.removeChild(UiManager.uiPanel);
			}
			if(RegRoleManager.rolereg && RegRoleManager.rolereg.parent)
			{
				RegRoleManager.rolereg.parent.removeChild(RegRoleManager.rolereg);
			}
			var loader:ModuleLoader = new ModuleLoader();
			loader.addEventListener(ModuleEvent.READY,onModuleReadyUseModuleManager);
			loader.loadModule("RpgUi.swf");
		}
		
		private function onModuleReadyUseModuleManager(event:Event):void{     
			var me:ModuleEvent = event as ModuleEvent; 
			var uiPanel:RpgUi = me.module.factory.create() as RpgUi;         
			Application.application.addChild(uiPanel);
			this.facade.registerMediator(new UIMediator(UIMediator.NAME,uiPanel));
		}
	}
}